﻿


$(document).ready(function () {


    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,basicWeek,basicDay'
        },
        editable: false,
        dayClick: function (date, allDay, jsEvent, view) {

            $("#dialog-modal").children().remove();
            var request = $.ajax({ url: get_URL_createBooking,
                type: "GET",
                data: { startDateSecs: Math.round(date.getTime() / 1000) },
                dataType: "html",
                async: false
            });
            request.done(function (ans) {
                $(ans).appendTo('#dialog-modal')
            });
            ShowBookingDialog();


        },
        eventClick: function (calEvent, jsEvent, view) {
            $("#dialog-modal").children().remove();
            var request = $.ajax({ url: get_URL,
                type: "GET",
                data: { idBooking: parseInt(calEvent.id) },
                dataType: "html",
                async: false
            });
            request.done(function (ans) {
                $(ans).appendTo('#dialog-modal')
            });
            ShowBookingDialog();
             

        },
        events: function (start, end, callback) {
            $.ajax({
                url: 'getevents',
                dataType: 'json',
                data: {
                    start: Math.round(start.getTime() / 1000),
                    end: Math.round(end.getTime() / 1000)
                },
                success: function (doc) {
                    var events = [];
                    $(doc).each(function () {
                        events.push({
                            id: $(this).attr('UniqueId'),
                            title: $(this).attr('Description'),
                            start: $.fullCalendar.parseDate(parseInt($(this).attr('CheckInUnixSecs'))),
                            end: $.fullCalendar.parseDate(parseInt($(this).attr('CheckOutUnixSecs'))),
                            color: $(this).attr('Color')
                        });
                    });
                    callback(events);
                }
            });
        }
    });

});

function functionDiscard() {
    var idBooking = $("#ModelId").val();
    if (idBooking != 0) {
        var request = $.ajax({ url: del_URL,
            type: "GET",
            data: { idBooking: parseInt(idBooking) },
            dataType: "html",
            async: false
        });
        request.done(function (ans) {
            alert('Successfully deleted');
        });
    }
    else {
        alert('Discarded')
    }
}

function ShowBookingDialog() {
    $("#dialog-modal").dialog({
        height: 420,
        width: 340,
        modal: true,
        buttons: {
            "Save": function () {
                var form = $('#dialog-modal').children("form");
                var request = $.ajax({
                    type: "POST",
                    url: form.attr('action'),
                    data: form.serialize(),
                    async: false
                });
                $(this).dialog("close");
                $('#calendar').fullCalendar('refetchEvents')
            },
            "Delete": function () {
                $(this).dialog("close");
                functionDiscard();
                $('#calendar').fullCalendar('refetchEvents')
            },
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function (sp) {

        }




    });
}